package com.drawing.service.impl;

import static com.drawing.client.QiNiuYunClient.saveImageToQNY;
import static org.apache.commons.lang3.StringUtils.isBlank;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.drawing.dao.ImageDAO;
import com.drawing.pojo.ImageInfo;
import com.drawing.pojo.param.ImageParam;
import com.drawing.service.IImageService;

/**
 * @author zhaozhenhang <zhaozhenhang@kuaishou.com>
 * Created on 2023-10-10
 */
@Service
public class ImageServiceImpl implements IImageService {

    @Autowired
    private ImageDAO imageDAO;

    @Override
    public ImageInfo saveImage(MultipartFile file, String fileName, String type) throws Exception {
        // 保存到七牛云
        String fileUrl = saveImageToQNY(file);
        if (isBlank(fileUrl)) {
            return new ImageInfo();
        }
        // 保存七牛云相关链接到数据库
        imageDAO.insert(fileUrl, fileName);
        ImageInfo info = new ImageInfo();
        info.setFileName(fileName);
        info.setFileUrl(fileUrl);
        return info;
    }

    @Override
    public List<ImageInfo> queryImage(ImageParam param) {
        return imageDAO.query(param);
    }
}
